package codegen.util.database;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Table implements Serializable, Comparable<Table> {

	private static final long serialVersionUID = -110700233559449347L;

	private String tableName;
	private String tableDesc;
	private int tableSeq;
	private List<Field> fieldList;
	private List<Field> primaryKeyList;

	public Table() {
		super();
	}

	public String getTableDesc() {
		return tableDesc;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public void setTableDesc(String tableDesc) {
		this.tableDesc = tableDesc;
	}

	public void setFieldList(List<Field> fieldList) {
		this.fieldList = fieldList;
	}

	public String getTableName() {
		return tableName;
	}

	public List<Field> getFieldList() {
		return fieldList;
	}

	public List<Field> getPrimaryKeyList() {
		return primaryKeyList;
	}

	public void setPrimaryKeyList(List<Field> primaryKeyList) {
		this.primaryKeyList = primaryKeyList;
	}

	public String getSetAssignListString() {
		return setAssignListString;
	}

	public String getKeyFieldListString() {
		return keyFieldListString;
	}

	public String getKeyFieldObjectListString() {
		return keyFieldObjectListString;
	}

	public String getKeyFieldClassListString() {
		return keyFieldClassListString;
	}

	public String getFieldListString() {
		return fieldListString;
	}

	public String getTable_fieldListString() {
		return table_fieldListString;
	}

	public String getSinglefieldListString() {
		return singlefieldListString;
	}

	public String getQuestionAssignKeyFieldListString() {
		return questionAssignKeyFieldListString;
	}

	public String getQuestionFieldListString() {
		return questionFieldListString;
	}

	public String getTypeFieldListString() {
		return typeFieldListString;
	}

	public String getKeyTypeFieldListString() {
		return keyTypeFieldListString;
	}

	public Table(String tableName) {
		this.tableName = tableName;
	}

	public Table(String tableName, String tableDesc) {
		this.tableName = tableName;
		this.tableDesc = tableDesc;
	}

	public Table(String tableName, String tableDesc, List<Field> fieldList) {
		this.tableName = tableName;
		this.tableDesc = tableDesc;
		this.fieldList = fieldList;
	}

	public String getClassName() {
		String s = tableName.replace("xm_", "");
		s = s.substring(0, 1).toUpperCase()
				+ s.substring(1).toLowerCase();
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '_') {
				s = s.substring(0, i) + (s.charAt(i + 1) + "").toUpperCase()
						+ s.substring(i + 2);
			}
		}
		s = s.replaceAll("Xml", "XML");
		s = s.replaceAll("Sdi", "SDI");
		return s;

	}

	public String getObjectName() {
		String s = this.getClassName();
		return s.substring(0, 1).toLowerCase() + s.substring(1);
	}

	public String getObjectName1() {
		String s = this.tableName.toLowerCase();
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '_') {
				s = s.substring(0, i) + (s.charAt(i + 1) + "").toUpperCase()
						+ s.substring(i + 2);
			}
		}
		s = s.replaceAll("Xml", "XML");
		s = s.replaceAll("Sdi", "SDI");
		return s;
	}

	public String getForeignTableString() {
		return foreignTableString;
	}

	public void setForeignTableString(String foreignTableString) {
		this.foreignTableString = foreignTableString;
	}

	public String getJoinString() {
		return joinString;
	}

	public void setJoinString(String joinString) {
		this.joinString = joinString;
	}

	private String foreignFieldString = ",";
	private String foreignTableString = "";
	private String joinString = "where ";

	public String fieldListString = "";
	public String table_fieldListString = "";
	
	
	public String singlefieldListString = "";
	public String inserfieldListString = "";
	public String insertvaluesString = "";
	public String updatefieldListString = "";
	public String conditionfieldliststring = "";
	
	

	public String table_fieldAliasString = "";
	public String typeFieldListString = "";
	public String questionFieldListString = "";
	public String keyFieldListString = "";
	public String keyFieldObjectListString = "";
	public String keyFieldClassListString = "";
	public String keyTypeFieldListString = "";
	public String questionAssignKeyFieldListString = "";
	public String setAssignListString = "SET ";

	
	
	public String getConditionfieldliststring() {
		return conditionfieldliststring;
	}

	public void setConditionfieldliststring(String conditionfieldliststring) {
		this.conditionfieldliststring = conditionfieldliststring;
	}

	public String getUpdatefieldListString() {
		return updatefieldListString;
	}

	public void setUpdatefieldListString(String updatefieldListString) {
		this.updatefieldListString = updatefieldListString;
	}

	public String getInsertvaluesString() {
		return insertvaluesString;
	}

	public void setInsertvaluesString(String insertvaluesString) {
		this.insertvaluesString = insertvaluesString;
	}

	public String getInserfieldListString() {
		return inserfieldListString;
	}

	public void setInserfieldListString(String inserfieldListString) {
		this.inserfieldListString = inserfieldListString;
	}

	public String toString() {
		return this.tableName + "-" + this.tableDesc;
	}

	public int compareTo(Table table) {
		return this.toString().compareTo(((Table) table).toString());
	}

	public int getTableSeq() {
		return tableSeq;
	}

	public void setTableSeq(int tableSeq) {
		this.tableSeq = tableSeq;
	}

	public List<Field> getStringFieldList() {
		List<Field> returnList = new ArrayList<Field>();
		for (Field field : fieldList) {
			if (field.getDataType().getTypeString().equals("String")
					&& field.getWidth() > 10) {
				returnList.add(field);
			}
		}
		return returnList;
	}

	public List<Field> getNumberFieldList() {
		List<Field> returnList = new ArrayList<Field>();
		for (Field field : fieldList) {
			if (field.getDataType().getTypeString().equals("BigDecimal")
					|| field.getDataType().getTypeString().equals("Double")
					|| field.getDataType().getTypeString().equals("Integer")) {
				returnList.add(field);
			}
		}
		return returnList;
	}

	public List<Field> getDateFieldList() {
		List<Field> returnList = new ArrayList<Field>();
		for (Field field : fieldList) {
			if (field.getDataType().getTypeString().equals("java.sql.Date")
					|| field.getDataType().getTypeString()
							.equals("java.sql.Timestamp")) {
				returnList.add(field);
			}
		}
		return returnList;
	}

	public String getTable_fieldAliasString() {
		return table_fieldAliasString;
	}

	public void setTable_fieldAliasString(String table_fieldAliasString) {
		this.table_fieldAliasString = table_fieldAliasString;
	}

	public String getForeignFieldString() {
		return foreignFieldString;
	}

	public void setForeignFieldString(String foreignFieldString) {
		this.foreignFieldString = foreignFieldString;
	}
}
